package com.yaowk.common.cloud.comfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yaowk.common.cloud.exception.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Slf4j
@Configuration
public class FeignErrorDecoderConfig implements ErrorDecoder {
    @Override
    public Exception decode(String s, Response response) {
        try {
            String body = Util.toString(response.body().asReader());
            JSONObject data = JSON.parseObject(body);
            log.info(body);
            return new FeignException(data.getString("message"), body);
        } catch (IOException e) {
            log.info("", e);
            return new FeignException("Feign 异常返回数据获取失败");
        }
    }
}
